package com.example.PDFdemo.Test;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.itextpdf.text.DocumentException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class T {
    public static void main(String[] args) throws DocumentException, IOException {

        String oldFilePath = "C:\\Users\\admin\\Desktop\\非空文档.pdf";
        String newFilePath = "C:\\Users\\admin\\Desktop\\test2.pdf";

        //字体样式转换
        Map<String, Integer> style = new HashMap<>();
        style.put("normal", 0);
        style.put("italic", 1);
        style.put("bold", 1);
        style.put("horizontal-left", 0);
        style.put("horizontal-center", 1);
        style.put("horizontal-right", 2);
        style.put("vertical-flex-start", 4);
        style.put("vertical-center", 5);
        style.put("vertical-flex-end", 6);

        //解析json
        String jsonString = "[{\"p\":0,\"w\":0.14280584473920627,\"h\":0.013271971496437053,\"x\":0.42875,\"y\":0.9000178147268408,\"order\":0,\"type\":\"text\",\"content\":\"987654321\",\"style\":{\"attName\":\"单行文本\",\"fontWeight\":\"normal\",\"color\":\"#000000\",\"fontStyle\":\"normal\",\"textAlign\":\"center\",\"fontSize\":\"12\",\"lineHeight\":\"15\",\"fontFamily\":\"宋体\"}},{\"p\":0,\"w\":0.145,\"h\":0.08671021377672208,\"x\":0.14125,\"y\":0.8283491686460808,\"order\":0,\"type\":\"multText\",\"content\":\"123456789\",\"style\":{\"attName\":\"多行文本\",\"fontWeight\":\"normal\",\"color\":\"#000000\",\"fontStyle\":\"normal\",\"textAlign\":\"center\",\"alignItems\":\"center\",\"justifyContent\":\"center\",\"fontSize\":\"12\",\"fontFamily\":\"宋体\"}},{\"p\":0,\"w\":0.14375,\"h\":0.1574940617577197,\"x\":0.42875,\"y\":0.7434085510688836,\"order\":0,\"type\":\"image\",\"imgUrl\":\"https://testyinji.gaoshan.co/oss/gs2021/20221116/18061450375d4ecec9372960f7a9505d1d97fcbcad365627471efdebacde9cd8.jpeg\",\"style\":{\"attName\":\"图片\"}}]";
        JSONArray contents = JSONUtil.parseArray(jsonString);
        List<PDFModels> modelList = contents.toList(PDFModels.class);
        List<FillInfo> fillInfos = new ArrayList<>();
        for (PDFModels model : modelList) {
            fillInfos.add(
                    FillInfo.builder()
                            .w(model.getW())
                            .h(model.getH())
                            .x(model.getX())
                            .y(model.getY())
                            .pageNumber(model.getP() + 1)
                            .fontName(model.getStyle().getFontFamily())
                            .fontSize(model.getStyle().getFontSize())
                            .fontBold(style.get(model.getStyle().getFontWeight()))
                            .fontIncline(style.get(model.getStyle().getFontStyle()))
                            .color(model.getStyle().getColor())
                            .imgUrl(model.getImgUrl())
                            .horizontal(style.get("horizontal-" + model.getStyle().getTextAlign()))
                            .vertical(style.get("vertical-" + model.getStyle().getAlignItems()))
                            .content(model.getContent())
                            .type(model.getType())
                            .build()
            );
        }
        System.out.println("1111111111111111111111111111111");
        PDFUtil.fillPdf(oldFilePath, newFilePath, fillInfos);
    }
}
